﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.Script.Serialization;
using Kooboo.Commerce.AddIn;
using Kooboo.Commerce;


namespace Kooboo.Web.Areas.Commerce.Web.UI
{
    public static class Serializer
    {

        public static string Serialize(this IEnumerable<IWebControl> controls)
        {
            var members = new List<string>();

            foreach (var i in controls)
            {
                members.Add(i.Name + ":" + i.DataFormats.Select(m=>m.ToString()).SerializeToJson());
            }

            
            return "{"+ string.Join(",", members) + "};";
        }

        public static string Serialize(this IEnumerable<IValidator> validators)
        {
            var members = new List<string>();

            foreach (var i in validators)
            {
                members.Add(i.Name + ":" + i.GetParameters().Select(m => new { m.Name, m.Example }).SerializeToJson());
            }

            return "{" + string.Join(",", members) + "}";
        }

        public static string Serialize(this IEnumerable<IValidationRule> rules)
        {
            var members = new List<string>();

            foreach (var rule in rules)
            {
                var item = "{ Id:"+ rule.Id.ToString() 
                    +", ValidatorName:" + rule.Validator.Name.SerializeToJson() 
                    + ",ErrorMessage:" + rule.ErrorMessage.SerializeToJson() 
                    + ",Parameters: "
                    + rule.Parameters.Select(m => new { m.Name, m.Value }).ToList().SerializeToJson()
                    +"}";

                members.Add(item);
            }
            return "[" + string.Join(",",members) + "]";
        }

        public static string Serialize(this IEnumerable<IProductAttribute> attributes)
        {
            return attributes.Select(i => new
            {
                i.Id,
                i.Name,
                i.Selections,
                WebControlName = i.WebControl.Name,
                i.DataFormat,
                i.DefaultValues,
                ProductTypeId = i.ProductType.Id,
                ValidationRules = i.ValidationRules.Select(n=> new 
                {
                 n.Id,
                 n.Validator.Name,
                 n.ErrorMessage,
                 Parameters = n.Parameters.Select(m=>new 
                 {
                     m.Name,
                     m.Example,
                     m.Value
                 }).ToArray(),
                }).ToArray(),
            }).SerializeToJson();
        }

        //public static string Serialize(this IEnumerable<IVariation> varations)
        //{
        //    return varations.Select(i => new {  ProductTypeId = i.ProductType.Id, i.Id, i.Name }).SerializeToJson(); ;
        //}

        //public static string Serialize(this IEnumerable<IBusinessType> businessTypes)
        //{
        //    return businessTypes.Select(i => new {i.Id, i.Name }).SerializeToJson();
        //}

        //public static string Serialize(this IEnumerable<IProductVariation> varitations)
        //{
        //    var list = varitations.Select(i => new 
        //    { 
        //        Id = i.Id, 
        //        Variations = i.ProductVariationItems.Select(n => new 
        //        {
        //            Id = n.Variation.Id,
        //            Value = n.Value            
        //        }).ToList(), 
        //    i.PurchasePrice,
        //    i.RetailPrice, 
        //    i.SKU,i.Stock, 
        //    i.DeliveryDate 
        //    })
        //    .ToList();

        //    return list.SerializeToJson();
        //}

        //public static string Serialize(this IEnumerable<IVariation> varations, bool isNeedControlInfo)
        //{
        //    return varations.Select(i => new { i.Id, i.Name,IsMultiSelectable = i.BusinessType.FindControl() is IMultiSelectable }).SerializeToJson(); ;
        //}
        
        #region IProductImage

        public static List<IProductImage> Deserialize<T>(this string json) where T : IProductImage
        {
            var list = new List<IProductImage>();

            if (string.IsNullOrWhiteSpace(json))
            {
                json = "[]";
            }
            var items = JsonSerializer.Deserialize<List<ProductImage>>(json);

            items.ForEach(i => list.Add(i));
            return list;

        }

        public class ProductImage : IProductImage
        {
            public int Id
            {
                get;
                set;
            }

            public string Name
            {
                get;
                set;
            }

            public IProduct Product
            {
                get;
                set;
            }

            public bool CoverImage
            {
                get;
                set;
            }
        } 

        #endregion
    }
}